/**
 * 收入记录权限测试脚本
 * 用于验证各角色权限是否正确
 */

const { checkIncomeRecordViewPermission, checkIncomeRecordEditPermission, checkIncomeRecordDeletePermission, checkIncomeRecordAddPermission } = require('common/dh-common/permission-utils');

// 测试用户ID和角色
const testUsers = [
  { uid: 'admin_uid', role: 'admin', name: '超级管理员' },
  { uid: 'director_uid', role: 'director', name: '院长' },
  { uid: 'vice_director_uid', role: 'vice_director', name: '副院长' },
  { uid: 'finance_uid', role: 'finance', name: '财务人员' },
  { uid: 'head_nurse_uid', role: 'head_nurse', name: '护士长' },
  { uid: 'administrative_uid', role: 'administrative', name: '行政人员' },
  { uid: 'purchaser_uid', role: 'purchaser', name: '采购人员' },
  { uid: 'doctor_uid', role: 'doctor', name: '医生' },
  { uid: 'nurse_uid', role: 'nurse', name: '护士' },
  { uid: 'patient_uid', role: 'patient', name: '患者' }
];

// 测试记录所有者ID
const recordOwner = 'doctor_uid';

// 测试函数
async function testPermissions() {
  console.log('开始测试收入记录权限...\n');
  
  // 测试添加权限
  console.log('=== 测试添加权限 ===');
  for (const user of testUsers) {
    const result = await checkIncomeRecordAddPermission(user.uid);
    console.log(`${user.name}(${user.role}) 添加权限: ${result.hasPermission ? '✓' : '✗'} ${result.reason || ''}`);
  }
  
  // 测试查看权限 - 查看自己的记录
  console.log('\n=== 测试查看权限 - 查看自己的记录 ===');
  for (const user of testUsers) {
    const result = await checkIncomeRecordViewPermission(user.uid, user.uid);
    console.log(`${user.name}(${user.role}) 查看自己记录: ${result.hasPermission ? '✓' : '✗'} ${result.reason || ''}`);
  }
  
  // 测试查看权限 - 查看医生记录
  console.log('\n=== 测试查看权限 - 查看医生记录 ===');
  for (const user of testUsers) {
    const result = await checkIncomeRecordViewPermission(user.uid, recordOwner);
    console.log(`${user.name}(${user.role}) 查看医生记录: ${result.hasPermission ? '✓' : '✗'} ${result.reason || ''}`);
  }
  
  // 测试编辑权限 - 编辑自己的记录
  console.log('\n=== 测试编辑权限 - 编辑自己的记录 ===');
  for (const user of testUsers) {
    const result = await checkIncomeRecordEditPermission(user.uid, user.uid);
    console.log(`${user.name}(${user.role}) 编辑自己记录: ${result.hasPermission ? '✓' : '✗'} ${result.reason || ''}`);
  }
  
  // 测试编辑权限 - 编辑医生记录
  console.log('\n=== 测试编辑权限 - 编辑医生记录 ===');
  for (const user of testUsers) {
    const result = await checkIncomeRecordEditPermission(user.uid, recordOwner);
    console.log(`${user.name}(${user.role}) 编辑医生记录: ${result.hasPermission ? '✓' : '✗'} ${result.reason || ''}`);
  }
  
  // 测试删除权限 - 删除自己的记录
  console.log('\n=== 测试删除权限 - 删除自己的记录 ===');
  for (const user of testUsers) {
    const result = await checkIncomeRecordDeletePermission(user.uid, user.uid);
    console.log(`${user.name}(${user.role}) 删除自己记录: ${result.hasPermission ? '✓' : '✗'} ${result.reason || ''}`);
  }
  
  // 测试删除权限 - 删除医生记录
  console.log('\n=== 测试删除权限 - 删除医生记录 ===');
  for (const user of testUsers) {
    const result = await checkIncomeRecordDeletePermission(user.uid, recordOwner);
    console.log(`${user.name}(${user.role}) 删除医生记录: ${result.hasPermission ? '✓' : '✗'} ${result.reason || ''}`);
  }
  
  console.log('\n测试完成!');
}

// 导出测试函数
module.exports = { testPermissions };